//
//  SettingViewController.m
//  jiyujiaProgram
//
//  Created by NiKi on 15/11/16.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "SettingViewController.h"
#import "SetCell.h"

#import "LoginViewController.h"
#import "EaseMob.h"

@interface SettingViewController () <UITableViewDataSource,UITableViewDelegate>

@end

@implementation SettingViewController
{
    NSArray *_dataArray;
    UITableView *_tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"设置"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];

    [self initData];
    [self creatTableView];
    [self creatLogoutButton];
}

- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initData
{
    _dataArray = [[NSArray alloc] init];
    _dataArray = @[@"账户设置",@"推送设置",@"关于日语+",@"选择语言",@"隐私安全",@"其他",@"自定义通知",@"系统更新"];
}

- (void)creatTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 64, ECSCREEN_W - 20, ECSCREEN_H - 64 - 55) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];

    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = BG_COLOR;
    _tableView.tableFooterView = [[UIView alloc]init];

    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55.f;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 5)];
    view.backgroundColor = BG_COLOR;
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"setCell";
    SetCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"SetCell" owner:nil options:nil];
        for (NSObject *obj in arr) {
            if ([obj isKindOfClass:[SetCell class]]) {
                cell = (SetCell *)obj;
                if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                    [cell setSeparatorInset:UIEdgeInsetsZero];
                }
                if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                    [cell setLayoutMargins:UIEdgeInsetsZero];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                break;
            }
        }
    }
    if (indexPath.section == 7) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    cell.clipsToBounds = YES;
    cell.layer.cornerRadius = 3;
    return cell;
}


- (void)creatLogoutButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, ECSCREEN_H - 50, ECSCREEN_W - 20, 40);
    button.backgroundColor = NAVC_COLOR;
    button.clipsToBounds = YES;
    button.layer.cornerRadius = 5;
    [button setTitle:@"退出登录" forState:UIControlStateNormal];
    [button setTitleColor:ECCOLOR(255, 255, 255, 1) forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [button addTarget:self action:@selector(logoutClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)logoutClick:(UIButton *)button
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    
    EMError *error = nil;
    NSDictionary *info = [[EaseMob sharedInstance].chatManager logoffWithUnbindDeviceToken:YES error:&error];
    if (!error && info) {
        NSLog(@"退出成功%@",info);
        [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:NO];
        userInfo.autoLogin = NO;
        [userInfo saveUserInfoToSanbox];
        LoginViewController *vc = [[LoginViewController alloc] init];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
    }else{
        NSLog(@"退出失败%@",error);
    }
    
//block 异步回调
//    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
//        if (!error && info) {
//            NSLog(@"退出成功%@",info);
//            [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:NO];
//            userInfo.autoLogin = NO;
//            [userInfo saveUserInfoToSanbox];
//            LoginViewController *vc = [[LoginViewController alloc] init];
//            UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];
//            [self presentViewController:nav animated:YES completion:nil];
//        }else{
//            NSLog(@"退出失败%@",error);
//        }
//    } onQueue:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
